#!/bin/bash
# filelockperf

###SHELLPACK preamble filelockperf-bench 1

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam   --min-threads      FILELOCKPERF_MIN_THREADS
###SHELLPACK parseargParam   --max-threads      FILELOCKPERF_MAX_THREADS
###SHELLPACK parseargParam   --workloads        FILELOCKPERF_WORKLOADS
###SHELLPACK parseargParam   --iterations	FILELOCKPERF_ITERATIONS
###SHELLPACK parseargParam   --lockiters	FILELOCKPERF_LOCKITERS
###SHELLPACK parseargParam   --locktypes        FILELOCKPERF_LOCKTYPES
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required filelockperf-${VERSION}
###SHELLPACK init_complete

echo $FILELOCKPERF_WORKLOADS > $LOGDIR_RESULTS/workloads
echo $FILELOCKPERF_LOCKTYPES > $LOGDIR_RESULTS/locktypes
echo $FILELOCKPERF_ITERATIONS > $LOGDIR_RESULTS/iterations
IFS=',' read -a ALL_WORKLOADS <<< "$FILELOCKPERF_WORKLOADS"
IFS=',' read -a ALL_LOCKTYPES <<< "$FILELOCKPERF_LOCKTYPES"

for WORKLOAD in ${ALL_WORKLOADS[@]}
do
	###SHELLPACK threads_large_stride_begin $FILELOCKPERF_MIN_THREADS $FILELOCKPERF_MAX_THREADS $FILELOCKPERF_ITERATIONS
		monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
		###SHELLPACK iteration_begin $FILELOCKPERF_ITERATIONS
			for TYPE in ${ALL_LOCKTYPES[@]}
			do
				cmd="$WORKLOAD"
				if [ $TYPE == "single" ]; then
				    cmd+="01"
				elif [ $TYPE == "multi" ]; then
				    cmd+="02"
				else
				    die Invalid lock type $TYPE ... aborting.
				fi

				TMPFILE="tmp-${WORKLOAD}-${ITERATION}-${TYPE}-${NR_THREADS}"
				outfile="$LOGDIR_RESULTS/filelockperf-${WORKLOAD}-${ITERATION}-${TYPE}-$NR_THREADS.log"

				echo "Running $WORKLOAD $TYPE $NR_THREADS/$FILELOCKPERF_MAX_THREADS threads (run $ITERATION/$FILELOCKPERF_ITERATIONS)"
				rm -rf $TMPFILE # cleanup any bogus leftovers for previous runs.
				save_rc ./$cmd -n $NR_THREADS -l $FILELOCKPERF_LOCKITERS $TMPFILE | tee $outfile > /dev/null
				recover_rc
				rm -rf $TMPFILE

				if [ $? -ne 0 ]; then
				    die Failed to run $WORKLOAD $TYPE
				fi
			done
		###SHELLPACK iteration_end
		monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
	###SHELLPACK threads_stride_end
done

exit $SHELLPACK_SUCCESS
